package com.runjian.rundo.iot.agent.common.network.tcp.client.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.runjian.rundo.iot.agent.business.publisher.DeviceMessagePublisher;
import com.runjian.rundo.iot.agent.common.codec.DeviceMessageCodec;
import com.runjian.rundo.iot.agent.common.network.core.cache.TcpNetworkClientCache;
import com.runjian.rundo.iot.agent.common.network.core.config.NetworkProperties;
import com.runjian.rundo.iot.agent.common.network.tcp.client.TcpNetworkClientManager;
import com.runjian.rundo.iot.agent.common.network.tcp.client.handle.TcpClientMessageHandler;
import com.runjian.rundo.iot.agent.common.network.tcp.client.session.DefaultTcpClientDeviceSessionManager;
import com.runjian.rundo.iot.agent.common.network.tcp.client.session.TcpClientDeviceSessionManager;
import com.runjian.rundo.iot.agent.common.network.tcp.handle.TcpIdleEventHandler;
import com.runjian.rundo.iot.agent.common.network.tcp.parser.PayloadParser;
import com.runjian.rundo.iot.agent.common.thread.ScheduledExecutorFactory;
import org.springframework.core.annotation.Order;

/**
 * tcp客户端自动配置
 *
 * @author CaoQingHua
 *
 */
@EnableConfigurationProperties({TcpNetworkClientConfiguration.class, NetworkProperties.class})
@Configuration
@Order(1)
public class TcpNetworkClientAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(TcpClientDeviceSessionManager.class)
    public DefaultTcpClientDeviceSessionManager defaultTcpClientDeviceSessionManager() {
        return new DefaultTcpClientDeviceSessionManager();
    }

    @Bean
    @ConditionalOnMissingBean(TcpNetworkClientCache.class)
    public TcpNetworkClientCache tcpNetworkClientCache() {
        return new TcpNetworkClientCache();
    }

    @Bean
    @ConditionalOnMissingBean(TcpClientMessageHandler.class)
    public TcpClientMessageHandler tcpClientMessageHandler(TcpClientDeviceSessionManager sessionManager, DeviceMessageCodec codec,
        DeviceMessagePublisher deviceMessagePublisher, TcpNetworkClientCache tcpNetworkClientCache) {
        return new TcpClientMessageHandler(sessionManager, codec, deviceMessagePublisher, tcpNetworkClientCache);
    }

    @Bean
    @ConditionalOnMissingBean(TcpNetworkClientManager.class)
    public TcpNetworkClientManager tcpNetworkClientManager(ScheduledExecutorFactory taskScheduledExecutorFactory,
        TcpNetworkClientConfiguration tcpNetworkClientProperties, TcpClientDeviceSessionManager sessionManager,
        TcpIdleEventHandler idleEventHandler, DeviceMessageCodec codec, PayloadParser payloadParser, TcpClientMessageHandler messageHandler,
        TcpNetworkClientCache tcpNetworkClientCache) {
        return new TcpNetworkClientManager(taskScheduledExecutorFactory, tcpNetworkClientProperties, sessionManager, idleEventHandler,
            codec, payloadParser, messageHandler, tcpNetworkClientCache);
    }

}
